package com.example.service.common;

import com.example.pojo.KUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年09月09日 21:58:00
 * @Description
 */
@Component
@Slf4j
public class RedisSynServiceImpl implements IDataSynchronousService{

    private final static String USER_KEY = "hot:user:hash";

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insertData(KUser kUser) {
        if (Objects.nonNull(kUser)) {
            String userId = kUser.getId().toString();
            redisTemplate.opsForHash().put(USER_KEY, userId, kUser);
        }
    }

    @Override
    public void updateData(KUser before, KUser after) {
        if (Objects.nonNull(after)) {
            String userId = after.getId().toString();
            redisTemplate.opsForHash().put(USER_KEY, userId, after);
        }
    }

    @Override
    public void deleteData(KUser kUser) {
        if (Objects.nonNull(kUser)) {
            String userId = kUser.getId().toString();
            redisTemplate.opsForHash().delete(USER_KEY, userId);
        }
    }
}
